package com.dayaokeji.rhythmschoolstudent.client.home.meeting;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.MultiListFragment;
import com.dayaokeji.rhythmschoolstudent.client.home.meeting.adapter.MeetingListAdapter;
import com.dayaokeji.rhythmschoolstudent.event.UpdateMeetingEvent;
import com.dayaokeji.rhythmschoolstudent.event.DeleteMeetingEvent;
import com.dayaokeji.rhythmschoolstudent.utils.DateUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.MeetingApi;
import com.dayaokeji.server_api.domain.Meeting;
import com.dayaokeji.server_api.domain.UserInfo;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

import retrofit2.Call;

/**
 * 会议列表
 */
public class MeetingListFragment extends MultiListFragment<Meeting,BaseViewHolder> {

    private String id;

    public static MeetingListFragment newInstance() {

        Bundle args = new Bundle();

        MeetingListFragment fragment = new MeetingListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        UserInfo userInfo = UserUtils.getUserInfo();
        if (userInfo == null) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }
        id = userInfo.getId();
        setRecyclerView();
        setAdapter(new MeetingListAdapter());
        setOnListener();
    }

    private void setRecyclerView() {
        /*getRecyclerView().addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .colorResId(R.color.color_default_divider)
                .sizeResId(R.dimen.default_divider_height).build());
        getRecyclerView().setBackgroundColor(Color.WHITE);*/
    }

    private void setOnListener() {
        getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                try{
                    int size = adapter.getData().size();
                    if (position >= 0 && position < size) {
                        Intent intent = new Intent(getActivity(), MeetingDetailActivity.class);
                        intent.putExtra(MeetingDetailActivity.EXTRA_MEETING_ENTITY, (Serializable) adapter.getData().get(position));
                        startActivity(intent);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDelMeetingUpdate(DeleteMeetingEvent meetingEvent) {
        if (meetingEvent.getMeeting() != null) {
            // TODO: 2017/11/3 后续直接删除列表
            onRefresh();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAddMeetingEvent(UpdateMeetingEvent updateMeetingEvent) {
        onRefresh();
    }

    @Override
    protected Call createCall(int offset, int max) {
        return ApiUtils.getApi(MeetingApi.class).createTodayList(offset,id,DateUtils.format(DateUtils.getDayBegin(),DateUtils.FORMAT_DATE),max);
    }

    @Override
    protected Call createCall2(int offset, int max) {
        return ApiUtils.getApi(MeetingApi.class).userTodayList(offset,id,DateUtils.format(DateUtils.getDayBegin(),DateUtils.FORMAT_DATE),max);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
